<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <div class="hello">
    Hello word!
  </div>
  用户名：
  <input type="text" id="username"> 密码：
  <input type="password" id="password">
  <button id="login">登录</button>
  <br>
  <button onclick="join()">加入</button>



</body>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script src="https://cdn.bootcss.com/socket.io/2.0.4/socket.io.js"></script>
<script type="text/javascript">

  var contextPath = 'http://localhost:3000';


  // 登录
  $('#login').on('click', function () {
    var username = $('#username').val()
    var password = $('#password').val()
    $.ajax({
      type: 'post',
      url: '/api/login',
      data: { username: username, password: password },
      success: function (data) {
        console.log(data);
      },
      error: function (data) {
        console.log(data);
      }

    })
  })


  // websocket
  // var ws = new WebSocket('ws://localhost:3000')
  // console.log(ws.readyState)
  // ws.onopen = function () {
  //   ws.send('Hello Server!');
  // }
  var socket

  function join() {
    socket = io('ws://localhost:3000');
    socket.emit('join', { name: 'Tom' });
    socket.on('news', function (data) {
      console.log(data);
    });
  }



</script>

</html>